import time
import getpass
import Deadline.DeadlineConnect as Connect




# connectionObject = Connect.DeadlineCon(HOST, PORT)



class CGAIDeadline(object):
    
    def __init__(self,HOST,PORT):
        
        self.connectionObject = Connect.DeadlineCon(HOST,PORT)

        self.username = getpass.getuser()


    # 获取某人的所有Jobs
    def get_jobs_by_username(self,username,ids=[]):
        start = time.time()
        if ids:
            all_active_jobs = self.connectionObject.Jobs.GetJobs(ids)
        else:
            all_active_jobs = self.connectionObject.Jobs.GetJobs()

        status_map = {0:'Unknown',1:'Active',2:'Suspended',3:'Completed',4:'Failed',6:'Pending'}

        data_list = []
        for a in all_active_jobs:
            data = {}
            Stat = a.get('Stat',0)
            if Stat in [1,2,3,4,6]:
                Props = a.get('Props',{})
                user = Props.get('User','')
                ListedSlaves = Props.get('ListedSlaves',[])
                if user == username:
                    Name = Props.get('Name','')
                    _id = a.get('_id','')
                    data['Name'] = Name
                    data['_id'] = _id
                    data['Stat'] = Stat
                    data['ListedSlaves'] = ListedSlaves
                    data['status'] = status_map[Stat]
                    data_list.append(data)
        end = time.time()
        print('执行速度:',end-start)
        return data_list


    # 获取所有任务
    def get_all_jobs(self):
    
        all_jobs = self.connectionObject.Jobs.GetJobs()
        print(len(all_jobs))
        stats = []
        for i in all_jobs:
            stat = i.get('Stat','')
            if stat not in stats:
                stats.append(stat)
        
        print(stats)

        return all_jobs


    # 获取所有失败的任务
    def get_all_failed_jobs(self):
        all_failed_jobs = self.connectionObject.Jobs.GetJobsInState('Failed')
        return all_failed_jobs


       # 获取所有已完成的任务
    def get_all_completed_jobs(self):
        all_completed_jobs = self.connectionObject.Jobs.GetJobsInState('Completed')
        return all_completed_jobs     


       # 获取所有暂停的任务
    def get_all_completed_jobs(self):
        all_suspended_jobs = self.connectionObject.Jobs.GetJobsInState('Suspended')
        return all_suspended_jobs    


    # 获取所有正在依赖挂起的任务
    def get_all_pending_jobs(self):
        all_pending_jobs = self.connectionObject.Jobs.GetJobsInState('Pending')
        return all_pending_jobs  


    # 获取所有激活的任务，包括Queued 排队 and Rendering正在渲染任务
    def get_all_active_jobs(self):
        all_active_jobs = self.connectionObject.Jobs.GetJobsInState('Active')
        return all_active_jobs


    # 获取Active Jobs
    def get_active_jobs(self,username=None):
        start = time.time()

        all_active_jobs = self.connectionObject.Jobs.GetJobsInState('Active')

        status_map = {0:'Unknown',1:'Active',2:'Suspended',3:'Completed',4:'Failed',6:'Pending'}

        data_list = []
        for a in all_active_jobs:
            data = {}
            Stat = a.get('Stat',0)
            Props = a.get('Props',{})
            user = Props.get('User','')
            Name = Props.get('Name', '')
            _id = a.get('_id', '')
            data['Name'] = Name
            data['_id'] = _id
            data['Stat'] = Stat
            data['status'] = status_map[Stat]
            if username:
                if user == username:
                    data_list.append(data)
            else:
                data_list.append(data)
        end = time.time()
        print('执行速度:',end-start)
        return data_list



    """  *****   Tasks      *****  """


    """  *****   机器      *****  """

    #获取机器
    def get_workers(self):
        workers = self.connectionObject.Slaves.GetSlaveNames()
        return workers


    # 获取机器信息
    def get_worker_infos(self):
        infos = self.connectionObject.Slaves.GetSlavesSettings()
        return infos


    # # 设置黑名单
    # slaveList = ['TX-chenyi']
    def set_limit_workers(self,job_id,workers):
        result = self.connectionObject.Jobs.SetJobMachineLimitListedSlaves(job_id,workers)
        # print(result)     
        return result  



